package ij.umasuo.gui;

import ij.IJ;
import ij.Prefs;
import ij.umasuo.MyUserSettings;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * this window give the user a choice to choose his/her own settings that he/she used before.
 * @author liuquan
 *
 */
public class SettingChooseWindow extends JDialog {

	private static final long serialVersionUID = 2158955772211995718L;
	
	public static List<String> files = new ArrayList<String>();
	private JComboBox cbChooser = new JComboBox();
	private JButton btnOk = new JButton("OK");
	private String createString = "Create a new Setting File...";
	private JTextField textField =  new JTextField();
	private JLabel lblYourName = new JLabel("Your Name:");
	private boolean isCreateNew = false;
	static{
		//here we have already know the home dir of this software, so we can use this to get the files that we created before.
		if(Prefs.getHomeDir() == null || "".equals(Prefs.getHomeDir())){
			IJ.error("The home directory if null!!");
		}
	}
	public SettingChooseWindow(Frame parent){
		super(  parent,   "Choose setting file",   true);
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

		this.getRootPane().setDefaultButton(null);
		this.setTitle("Choose setting file");
		this.setLocation(parent.getX()+100, parent.getY() + 200);
		this.setSize(400, 150);
		this.setResizable(false);
//		getContentPane().setLayout(null);
		getContentPane().setLayout(null);
		cbChooser.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
//				add a listener to hide and show the text 
				if(cbChooser.getSelectedItem() !=null && arg0.getStateChange() == ItemEvent.SELECTED){
					if(createString.equals(cbChooser.getSelectedItem().toString())){
						//here to show the create text field
						textField.setVisible(true);
						lblYourName.setVisible(true);
					}else{
						//here to hide it
						textField.setVisible(false);
						lblYourName.setVisible(false);
					}
				}
			}
		});
//		cbChooser.setModel(new DefaultComboBoxModel(new String[] {"Choose Setting File..."}));
		cbChooser.setSize(374, 21);
		
		//add chooser to the window
		getContentPane().add(cbChooser);
		cbChooser.setLocation(10, 30);
		//get all the setting files and add them into the chooser		
		cbChooser.removeAllItems();
		File settingDir = new File(Prefs.getHomeDir() + "\\UData");
		if(!settingDir.exists()){
			//if the setting data folder dose not exits, then create it.
			settingDir.mkdirs();
		}
		File fileList[] = settingDir.listFiles();
		if(fileList.length == 0){
			isCreateNew = true;
			// if there is no file, we show the textfield
			textField.setVisible(true);
			lblYourName.setVisible(true);
		}else{
			isCreateNew = false;
			for(int i = 0;i<fileList.length;i++){
				cbChooser.addItem(fileList[i].getName());
			}
			textField.setVisible(false);
			lblYourName.setVisible(false);
		}
		//add the create new file txt into the chooser
		cbChooser.addItem(createString);
		btnOk.setBounds(250, 65, 90, 45);
		getContentPane().add(btnOk);
		//button action. here to load the selected file
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
//				IJ.error(cbChooser.getSelectedItem().toString());
				if(isCreateNew){
					String fileName = Prefs.getHomeDir() + "\\UData\\" + textField.getText() + ".properties";
//					IJ.error(fileName);
					File settingFile = new File(fileName);
					if(!settingFile.exists()){
						try {
							settingFile.createNewFile();
						} catch (IOException e) {
							IJ.error(e.getMessage());
						}
					}
					MyUserSettings.loadSettings(fileName);//load user's settings
				}else{
					String fileName =Prefs.getHomeDir() + "\\UData\\" +  cbChooser.getSelectedItem().toString();
					MyUserSettings.loadSettings(fileName);//load user's settings
				}
				close();
			}
		});
		textField.setBounds(72, 77, 150, 21);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		lblYourName.setBounds(8, 80, 66, 15);
		getContentPane().add(lblYourName);
		
		btnOk.setSelected(true);
	}
	
	public void close(){
		//close the window
		 this.dispose();
	}
}
